from datetime import datetime
from django.db import models
from django.contrib.auth.models import AbstractUser
from apps.books.models import BookInfo

class MyUser(AbstractUser):
    SEX = ((0, '男'),(1, '女'),)
    STATUS = ((0, '正常'),(1, '异常'),)

    true_name = models.CharField('真实姓名', blank=True, max_length=50)
    mobile = models.CharField('手机号码', max_length=11, default="")
    sex = models.IntegerField(default=0, choices=SEX)
    user_img = models.ImageField("头像", upload_to="user_img", default="default_user_img.png")
    status = models.IntegerField(default=0, choices=STATUS)
    create_time = models.DateTimeField(default=datetime.now, verbose_name='创建时间')
    update_time = models.DateTimeField(default=datetime.now, verbose_name="更新时间")

    def __str__(self):
        return self.username

    class Meta(AbstractUser.Meta):
        permissions = (
            ['check_myuser', '审核用户信息'],
        )

class ReadRecord(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(MyUser,null=True,blank=True,verbose_name='读者',on_delete=models.DO_NOTHING)
    book = models.ForeignKey(BookInfo,null=True,blank=True,verbose_name='图书',on_delete=models.DO_NOTHING)
    begin_time = models.DateTimeField(default=datetime.now,verbose_name='阅读开始时间')
    end_time = models.DateTimeField(default=datetime.now, null=True,blank=True, verbose_name='阅读结束时间')
    schedule_page = models.IntegerField(default=0,verbose_name='阅读进度页数')
    comments = models.TextField(default='', verbose_name='阅读评论')

    def __str__(self):
        return self.user + '-' + self.book.name

    class Meta:
        verbose_name = '阅读记录'
        verbose_name_plural = '阅读记录'
        db_table = 'read_record'